﻿@* 如果你要使用绑定，请删除本部分 *@
@section Scripts {
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "密码重置";

    var passwordResetToken = Request.Form["resetToken"] ?? Request.QueryString["resetToken"];

    bool tokenExpired = false;
    bool isSuccess = false;

    // 设置验证
    Validation.RequireField("newPassword", "“新密码”字段是必填的。");
    Validation.Add("confirmPassword",
        Validator.EqualsTo("newPassword", "新密码和确认密码不匹配。"));
    Validation.RequireField("passwordResetToken", "密码重置令牌字段是必填的。");
    Validation.Add("newPassword",
        Validator.StringLength(
            maxLength: Int32.MaxValue,
            minLength: 6,
            errorMessage: "新密码必须至少包含 6 个字符"));

    if (IsPost && Validation.IsValid()) {
        AntiForgery.Validate();
        var newPassword = Request["newPassword"];
        var confirmPassword = Request["confirmPassword"];

        if (WebSecurity.ResetPassword(passwordResetToken, newPassword)) {
            isSuccess = true;
        } else {
            ModelState.AddError("passwordResetToken", "密码重置令牌无效。");
            tokenExpired = true;
        }
    }
}

<hgroup class="title">
    <h1>@Page.Title.</h1>
    <h2>使用下面的表单重置密码。</h2>
</hgroup>

@if (!WebMail.SmtpServer.IsEmpty()) {
    if (!Validation.IsValid()) {
        <p class="validation-summary-errors">
            @if (tokenExpired) {
                <text>密码重置令牌错误或可能已过期。访问<a href="~/Account/ForgotPassword">“忘记密码”页</a> 
                以生成新密码。</text>
            } else {
                <text>无法重置密码。请更正错误并重试。</text>
            }
        </p>
    }

    if (isSuccess) {
        <p class="message-success">
            密码已更改!单击<a href="~/Account/Login" title="登录">此处</a>以登录。
        </p>
    }

    <form method="post">
        @AntiForgery.GetHtml()
        <fieldset>
            <legend>密码更改表单</legend>
            <ol>
                <li class="new-password">
                    <label for="newPassword" @if (!ModelState.IsValidField("newPassword")) {<text>class="error-label"</text>}>新密码</label> 
                    <input type="password" id="newPassword" name="newPassword" disabled="@isSuccess" @Validation.For("newPassword") />
                    @Html.ValidationMessage("newPassword")
                </li>
                <li class="confirm-password">
                    <label for="confirmPassword" @if (!ModelState.IsValidField("confirmPassword")) {<text>class="error-label"</text>}>确认密码</label> 
                    <input type="password" id="confirmPassword" name="confirmPassword" disabled="@isSuccess" @Validation.For("confirmPassword") />
                    @Html.ValidationMessage("confirmPassword")
                </li>
                <li class="reset-token">
                    <label for="resetToken" @if (!ModelState.IsValidField("resetToken")) {<text>class="error-label"</text>}>Password reset token</label> 
                    <input type="text" id="resetToken" name="resetToken" value="@passwordResetToken" disabled="@isSuccess" @Validation.For("resetToken") />
                    @Html.ValidationMessage("resetToken")
                </li>
            </ol>
            <input type="submit" value="重置密码" disabled="@isSuccess"/>
        </fieldset>
    </form>
} else {
    <p class="message-info">
        因为没有正确配置 SMTP 服务器，所以此网站禁用了
        密码恢复功能。若要重置密码，请与此站点的所有者
        联系。
    </p>
}
